using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection.Emit;

namespace EntLibContrib.Validation.Design.UI
{
    public partial class TypeNameUI : Form
    {
        Type t;

        public TypeNameUI()
        {
            InitializeComponent();
            txtTypeExists.ForeColor = Color.Black;
        }

        private void txtTypeName_Leave(object sender, EventArgs e)
        {
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                t = Type.GetType(txtTypeName.Text, false);
                if (t == null)
                {
                    AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.ReflectionOnly);
                    ModuleBuilder mb = ab.DefineDynamicModule("temp");
                    t = mb.DefineType(txtTypeName.Text);
                }
                DialogResult = DialogResult.OK;
                Close();
            }
            catch
            {
                txtTypeExists.ForeColor = Color.Red;
            }
        }

        public Type Type
        {
            get { return t; }
        }

        private void txtTypeName_Enter(object sender, EventArgs e)
        {
            txtTypeExists.ForeColor = Color.Black;
        }

        private void txtTypeName_TextChanged(object sender, EventArgs e)
        {
            txtTypeExists.Visible = (Type.GetType(txtTypeName.Text, false) != null);
        }

    }
}